package com.yun.zf.app.controller;

import cn.hutool.core.date.DateUtil;
import com.yun.zf.app.service.SysUserOrderService;
import com.yun.zf.app.service.ZfHouseBasicService;
import com.yun.zf.app.service.ZfReportService;
import com.yun.zf.common.annotation.LogPrint;
import com.yun.zf.common.app.domain.ZfReport;
import com.yun.zf.common.app.dto.center.MyCouponListDto;
import com.yun.zf.common.app.dto.center.OrderListDto;
import com.yun.zf.common.app.model.center.ReportMo;
import com.yun.zf.common.app.model.home.RecordHouseBasicInfoMo;
import com.yun.zf.common.constant.Constants;
import com.yun.zf.common.mapstruct.app.ReportMapper;
import com.yun.zf.common.vo.Response;
import com.yun.zf.system.security.utils.SecurityUtils;
import com.yun.zf.system.service.SysCouponReceiveService;
import com.yun.zf.system.service.SysUserPhoneService;
import com.yun.zf.system.utils.AliyunOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "个人中心")
@RequestMapping("/center")
public class PersonCenterController {

    @Autowired
    private SysCouponReceiveService couponReceiveService;
    @Autowired
    private ZfHouseBasicService basicService;
    @Autowired
    private SysUserPhoneService phoneService;
    @Autowired
    private SysUserOrderService userOrderService;
    @Autowired
    private AliyunOssUtil ossUtil;
    @Autowired
    private ZfReportService reportService;

    @PostMapping("/invite")
    @ApiOperation(value = "我的优惠券列表")
    public Response<Map<Integer, List<MyCouponListDto>>> communitySearchSuggest() {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        Map<Integer, List<MyCouponListDto>> coupons = couponReceiveService.getCouponList(userId);
        return Response.success(coupons);
    }

    @PostMapping("/myOrder")
    @ApiOperation(value = "我的订单")
    public Response<List<OrderListDto>> myOrder() {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        List<OrderListDto> list = userOrderService.getMyOrder(userId);
        return Response.success(list);
    }

    @PostMapping("/myPact")
    @ApiOperation(value = "我的合同")
    public Response myPact() {
        //todo 。 我的合同通过拿到订单号,在通过订单号和城市拿到 zf_pact_pdf 中的文件链接预览显示
        return Response.success();
    }

    @PostMapping("/recordHouse")
    @ApiOperation(value = "我是业主-委托房源")
    public Response recordHouse(@RequestBody RecordHouseBasicInfoMo houseBasicInfo) {
        try {
            int count = basicService.recordHouseBasic(houseBasicInfo);
            if (count > 0) {
                return Response.success("委托成功");
            }
        } catch (Exception e) {
            return Response.success("委托异常，请稍后重试");
        }
        return Response.success("委托异常，请稍后重试");
    }

    @PostMapping("/bindPhone")
    @ApiOperation(value = "绑定手机号码")
    @LogPrint(description = "绑定手机号码")
    public Response bindPhone(@RequestBody Map<String, String> map) {
        // 绑定手机号码前期不做验证码验证
        int c = phoneService.bindPhone(map.get("iphone"));
        if (c > 0) {
            return Response.success("绑定成功");
        }
        return Response.error("绑定失败");
    }


    @PostMapping("/uploadFile")
    @ApiOperation(value = "上传签约人证件以及补充文件信息")
    @LogPrint(description = "上传签约人证件以及补充文件信息")
    public Response uploadPactFile(@RequestParam("files") MultipartFile[] files, HttpServletRequest request) {
        try {
            List<Map<String, String>> pictureList = new ArrayList<>();
            if (files != null && files.length > 0) {
                for (MultipartFile file : files
                ) {
                    Map<String, String> map = ossUtil.uploadObject2OSS(file, Constants.BUCKET_REPORT, Constants.FOLDER_REPORT + "/" + DateUtil.format(new Date(), "yyyyMMdd"));
                    if (map != null) {
                        pictureList.add(map);
                    }
                }
            }
            return Response.success(pictureList);
        } catch (Exception e) {
            return Response.error();
        }
    }


    @PostMapping("/report")
    @ApiOperation(value = "违规举报")
    @LogPrint(description = "违规举报")
    public Response report(@RequestBody ReportMo reportMo) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        ZfReport report = ReportMapper.INSTANCE.moToReport(reportMo);
        report.setCreateTime(new Date());
        report.setUserId(userId);
        report.setStatus(1);
        if (reportService.save(report)) {
            return Response.success("举报成功，请等待审核结果");
        }
        return Response.error("举报失败，请稍后再试");
    }

}
